//
//  SEGActivityOffLineInfoCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/11.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityOffLineInfoCell.h"
@interface SEGActivityOffLineInfoCell ()<UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, strong)UILabel *remarkLabel;

@end

@implementation SEGActivityOffLineInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;

    UILabel *nameLable = [self createLabelWithIndex:0 text:@"姓名"];
    self.nameField = [self createTextFieldWithLabel:nameLable placeholder:@"请输入您的姓名"];
    
    UILabel *sexLable = [self createLabelWithIndex:1 text:@"性别"];
    self.maleButton = [self createSelectedButton];
    [self.maleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sexLable.mas_right).offset(20);
        make.centerY.equalTo(sexLable);
    }];
    UILabel *maleLable = [[UILabel alloc] init];
    maleLable.font = kFont_13;
    maleLable.textColor = kColor_1;
    maleLable.text = @"男";
    [self.contentView addSubview:maleLable];
    [maleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.maleButton.mas_right).offset(10);
        make.centerY.equalTo(sexLable);
    }];
    self.maleButton.selected = YES;
    
    self.femaleButton = [self createSelectedButton];
    [self.femaleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(maleLable.mas_right).offset(30);
        make.centerY.equalTo(sexLable);
    }];
    UILabel *femaleLabel = [[UILabel alloc] init];
    femaleLabel.font = kFont_13;
    femaleLabel.textColor = kColor_1;
    femaleLabel.text = @"女";
    [self.contentView addSubview:femaleLabel];
    [femaleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.femaleButton.mas_right).offset(10);
        make.centerY.equalTo(sexLable);
    }];
    
    UILabel *phoneLable = [self createLabelWithIndex:2 text:@"手机"];
    self.phoneField = [self createTextFieldWithLabel:phoneLable placeholder:@"请输入您的手机号码"];
    self.phoneField.text = kAccountMobile;
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    
    
    UILabel *numberLable = [self createLabelWithIndex:3 text:@"人数"];
    self.numberField = [self createTextFieldWithLabel:numberLable placeholder:@"请输入您的报名人数"];
    self.numberField.keyboardType = UIKeyboardTypeNumberPad;
    
    UILabel *lable = [[UILabel alloc] init];
    lable.font = kFont_13;
    lable.textColor = kColor_1;
    lable.text = @"备注";
    [self.contentView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(numberLable.mas_bottom).offset(15);
    }];
    
    // 描述输入框
    self.remarkView = [[UITextView alloc] init];
    self.remarkView.font = kFont_13;
    self.remarkView.textColor = kColor_1;
    self.remarkView.returnKeyType = UIReturnKeyDone;
    self.remarkView.tintColor = kColor_T;
    [self.contentView addSubview:self.remarkView];
    [self.remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberLable.mas_bottom);
        make.left.equalTo(self.numberField).offset(-2);
        make.right.equalTo(self.contentView);
        make.height.mas_equalTo(80);
        make.bottom.equalTo(self.contentView);
    }];
    self.remarkView.textContainerInset = UIEdgeInsetsMake(15, 0, 15, 15);
    self.remarkView.delegate = self;
    
    //提示
    UILabel *remarkLabel = [[UILabel alloc] init];
    remarkLabel.text = @"请按活动规则填写备注信息......";
    remarkLabel.font = kFont_13;
    remarkLabel.textColor = kColor_C;
    [self.contentView addSubview:remarkLabel];
    [remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numberField);
        make.right.equalTo(self.remarkView);
        make.top.equalTo(lable);
    }];
    self.remarkLabel = remarkLabel;
    
}

- (UILabel *)createLabelWithIndex:(NSInteger)index text:(NSString *)text
{
    UILabel *lable = [[UILabel alloc] init];
    lable.font = kFont_13;
    lable.textColor = kColor_1;
    NSString *string = [NSString stringWithFormat:@"%@ *",text];
    NSMutableAttributedString* str2 = [[NSMutableAttributedString alloc] initWithString:string];
    [str2 addAttribute:NSForegroundColorAttributeName value:kColor_S1 range:NSMakeRange([str2 length] - 1, 1)];
    lable.attributedText = str2;
    [self.contentView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView).offset(44.0 * index);
        make.height.mas_equalTo(44.0);
        make.width.mas_equalTo(40);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(lable.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    return lable;
}

- (UITextField *)createTextFieldWithLabel:(UILabel *)label placeholder:(NSString *)placeholder
{
    UITextField *textField = [[UITextField alloc] init];
    textField.font = kFont_13;
    textField.textColor = kColor_1;
    textField.tintColor = kColor_T;
    textField.placeholder = placeholder;
    [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:textField.placeholder ?: @"" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
    textField.returnKeyType = UIReturnKeyDone;
    textField.delegate = self;
    [self.contentView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(17);
        make.centerY.equalTo(label);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.equalTo(label);
    }];
    return textField;
}

- (UIButton *)createSelectedButton
{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
    return button;
}

- (void)buttonPressed:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if ([self.maleButton isEqual:sender]) {
        self.femaleButton.selected = !self.femaleButton.selected;
    }else {
        self.maleButton.selected = !self.maleButton.selected;
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView*)textView
{
    if ([[textView text] length] > 0) {
        [self.remarkLabel setHidden:YES];
    }
    else {
        [self.remarkLabel setHidden:NO];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        [self.remarkView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

@end
